﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using huqiang.Collections;
using huqiang.Communication;

namespace huqiang.Framework
{
    public class MainThreadMissionManager : HFBase
    {
        protected override string TypeName { get => "MainThreadMissionManager"; }
        struct Mission: IClear
        {
            public object obj;
            public Action<object> action;
            public void Clear()
            {
                obj = null;
                action = null;
            }
        }
        LockQueueS<Mission> buffer = new LockQueueS<Mission>(256);
        public void AddMainMission(Action<object> action, object obj)
        {
            Mission mission = new Mission();
            mission.action = action;
            mission.obj = obj;
            buffer.Enqueue(mission);
        }
        protected override void Update(float time)
        {
            for(int i=0;i<buffer.Count;i++)
            {
                var miss = buffer.Dequeue();
                if (miss.action != null)
                {
                    try
                    {
                        miss.action(miss.obj);
                    }catch(Exception ex)
                    {
                        UnityEngine.Debug.LogError(ex.StackTrace);
                    }
                }     
            }
        }
    }
}
